import unittest
from main import count_chars, count_words, count_sentences, analyze_code_content


class TestWordCounterFinalVersion(unittest.TestCase):
  

    #基础功能测试
    def test_basic_functions(self):
        """测试字符数、单词数、句子数"""
        text_sample = "Hello world! This is test text.\nIs it good? Yes, it is... Mr. Smith thinks so."
        self.assertEqual(count_chars(text_sample), len(text_sample))
        self.assertEqual(count_words(text_sample), 16)
        self.assertEqual(count_sentences(text_sample), 6)
        self.assertEqual(count_sentences("Wait... Really?! Yes."), 3)

    #Python代码分析测试
    def test_python_simple(self):
        """基础 Python 代码统计"""
        code = (
            "# 单行注释\n"
            "import os\n"
            "\n"
            "def f():\n"
            '    """Docstring here"""\n'
            "    return 1\n"
        )
        result = analyze_code_content(code, "sample.py")
        print("\n[PY_SIMPLE]", result)
        self.assertEqual(result["total"], 6)
        self.assertEqual(result["blank"], 1)
        self.assertEqual(result["comment"], 2)
        self.assertEqual(result["code"], 3)

    def test_python_multiline_docstring(self):
        """测试 Python 多行 docstring"""
        code = (
            "class A:\n"
            "    '''\n"
            "    多行注释1\n"
            "    多行注释2\n"
            "    '''\n"
            "    def func(self):\n"
            "        pass\n"
        )
        result = analyze_code_content(code, "test.py")
        print("\n[PY_MULTILINE]", result)
        self.assertEqual(result["total"], 7)
        self.assertEqual(result["comment"], 4)
        self.assertEqual(result["code"], 3)
        self.assertEqual(result["blank"], 0)

    def test_python_inline_comment(self):
        """测试 Python 行内注释"""
        code = (
            "a = 1  # inline comment\n"
            "b = 2\n"
            "# only comment\n"
            "\n"
            "print(a + b)\n"
        )
        result = analyze_code_content(code, "inline.py")
        print("\n[PY_INLINE]", result)
        self.assertEqual(result["total"], 5)
        self.assertEqual(result["code"], 3)
        self.assertEqual(result["comment"], 1)
        self.assertEqual(result["blank"], 1)

    #C语言分析测试
    def test_c_code_basic(self):
        """C语言基本代码结构"""
        code = (
            "#include <stdio.h>\n"
            "\n"
            "// 单行注释\n"
            "/* 多行注释开始\n"
            "   第二行注释 */\n"
            "int main() {\n"
            "    return 0;\n"
            "}\n"
        )
        result = analyze_code_content(code, "main.c")
        print("\n[C_BASIC]", result)
        self.assertEqual(result["total"], 8)
        self.assertEqual(result["blank"], 1)
        self.assertEqual(result["comment"], 3)
        self.assertEqual(result["code"], 4)

    def test_c_with_macros(self):
        """C语言含预处理指令（应视为代码）"""
        code = (
            "#include <stdio.h>\n"
            "#define MAX 100\n"
            "// comment\n"
            "int main() {\n"
            "    printf(\"%d\", MAX);\n"
            "}\n"
        )
        result = analyze_code_content(code, "macro.c")
        print("\n[C_MACRO]", result)
        self.assertEqual(result["comment"], 1)
        self.assertIn("#define", code)
        self.assertGreater(result["code"], result["comment"])

    def test_c_multiline_edge_case(self):
        """C语言：多行注释未正确闭合"""
        code = (
            "/* start comment\n"
            "line two\n"
            "int main() {\n"
            "   return 0;\n"
            "}\n"
        )
        result = analyze_code_content(code, "broken.c")
        print("\n[C_BROKEN]", result)
        self.assertTrue(result["comment"] >= 2)
        self.assertTrue(result["code"] <= 2)

    #混合与极端情况测试
    def test_empty_and_whitespace(self):
        """空文件与纯空白文件"""
        result = analyze_code_content("", "empty.py")
        self.assertEqual(result["total"], 0)
        self.assertEqual(result["blank"], 0)
        result = analyze_code_content("   \n   \n", "space.c")
        self.assertEqual(result["blank"], 2)

    def test_comment_only_files(self):
        """纯注释文件 (Python/C)"""
        py_comments = "# test\n# another\n'''multi\nline\ncomment'''\n"
        c_comments = "// a\n/* c-style\ncomment */\n"
        r1 = analyze_code_content(py_comments, "only.py")
        r2 = analyze_code_content(c_comments, "only.c")
        print("\n[COMMENT_ONLY_PY]", r1, "\n[COMMENT_ONLY_C]", r2)
        self.assertEqual(r1["code"], 0)
        self.assertEqual(r2["code"], 0)
        self.assertTrue(r1["comment"] > 0)
        self.assertTrue(r2["comment"] > 0)

    def test_trailing_comment(self):
        """代码末尾为注释"""
        code = (
            "int a = 0;\n"
            "int b = 1;\n"
            "// end comment\n"
        )
        result = analyze_code_content(code, "end.c")
        self.assertEqual(result["comment"], 1)
        self.assertEqual(result["code"], 2)
        self.assertEqual(result["total"], 3)


if __name__ == "__main__":
    unittest.main(verbosity=2)
